package com.hitqz.robot.dispatch.netty.dispatch;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.cache.Cache;
import com.hitqz.robot.api.business.dto.RobotClientSummaryDto;
import com.hitqz.robot.api.business.dto.RobotControlButtonDto;
import com.hitqz.robot.api.business.dto.RobotSendClientMsgDto;
import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.api.business.enums.WorkStatusEnum;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.daemon.quartz.constants.RobotQuartzEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 机器人/AGV空闲充电
 * @author xupkun
 * @date 2025/1/5
 */

@Slf4j
@Component
public class ChargeDispatch {


    /**
     * 空闲30min
     * @return
     */
    public static String dispatch() {
        Cache<String, Long> idleTask = SpringUtil.getBean("idleTask");
        Long current = System.currentTimeMillis();

        for (String key : idleTask.asMap().keySet()) {
            Long idleTime = idleTask.getIfPresent(key);
            // 30 分钟空闲
            if (current - idleTime > (30 * 60 * 1000)) {
                // 30min
                // 获取机器人组服务
                // 下发充电的命令
                RobotService robotService = SpringUtil.getBean("robotServiceImpl");
                RobotEntity robotEntity = robotService.selectByRobotSn(key);
                if (robotEntity != null) {
                   Cache<String, RobotClientSummaryDto> posInfoDtoCache = SpringUtil.getBean("robotCache");
                    RobotClientSummaryDto robotClientSummaryDto = posInfoDtoCache.getIfPresent(robotEntity.getRobotSn());
                    if (robotClientSummaryDto != null && robotClientSummaryDto.getStatus() != null) {
                        if (robotClientSummaryDto.getLogicOnlineStatus() == 1 &&
                                "放电".equals(robotClientSummaryDto.getStatus().getCurrentDirection())) {
                            if (WorkStatusEnum.FREE.getCode().equals(String.valueOf(robotClientSummaryDto.getStatus().getWorkStatus())) &&
                                    (Double.parseDouble(robotClientSummaryDto.getStatus().getBattery()) <= 85D)
                            ) {
                                if (robotClientSummaryDto.getStatus().getEmergencyStop() == 1) {
                                    log.info("急停触发不下发充电");
                                    continue;
                                }
                                log.info("机器人 {} 空闲30min，开始充电", robotEntity.getRobotSn());
                                RobotSendClientMsgDto msgDto = new RobotSendClientMsgDto();
                                msgDto.setCmd(21000);
                                msgDto.setClientId(robotEntity.getRobotSn());
                                RobotControlButtonDto robotControlButtonDto = new RobotControlButtonDto();
                                robotControlButtonDto.setAction("charge");
                                msgDto.setData(robotControlButtonDto);
                                SpringUtil.getApplicationContext().publishEvent(msgDto);
                                idleTask.put(key, System.currentTimeMillis());
                                log.info("下发充电命令后重置倒计时");
                            }
                        }
                    }
                }
            }
        }
        return RobotQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }


}
